/**
 * Created by Administrator on 2017/6/1.
 */
export const slide = function (id, Tagname) {
  let imgbox = getId(id);
  let imgList = imgbox.getElementsByTagName(Tagname);
  let len = imgList.length - 1;
  let offsetWidth = imgList[0].offsetWidth;
  let now = 0;

  setInterval(function () {
    if (now < len) {
      now++;
    } else {
      now = 0;
    }
    doMove(imgbox, -now * offsetWidth);
  }, 3000);
};

function doMove (obj, target) {
  let timer = setInterval(function () {
    let cur = parseInt(obj.offsetLeft);
    console.log(cur + ' ' + target);
    let speed = (target - cur) / 10;
    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

    if (cur === target) {
      clearInterval(timer);
    } else {
      obj.style.left = cur + speed + 'px';
    }
  }, 50);
}

// 获取样式值
// function getStyle(obj, attr) {
//   if (obj.currentStyle) {
//       return obj.currentStyle[attr];
//   } else {
//     return getComputedStyle(obj, false)[attr];
//   }
// }

// 获取id值
function getId (id) {
  return id ? document.getElementById(id) : null;
}

